package client;

import defaultProperties.window;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * chat.java
 *
 * Created on Sep 30, 2013, 1:36:49 PM
 */
/**
 *
 * @author GON
 */
public class chat extends window implements Runnable {

    private String serverIP;
    private String username;
    private int serverPort;
    private Socket socket;
    private BufferedReader br;
    private BufferedWriter bw;
    boolean connected = false;
    int posX = 0, posY = 0;
    boolean isGroup = false;
    boolean settingsShown = false;
    addMembersToGroup amtg;
    ArrayList groupMembers = new ArrayList();
    ArrayList everyone = new ArrayList();
    private int panelHeight = 100;

    /** Creates new form chat */
    public chat(String name, Socket s, String uname, Boolean isgroup, ArrayList gMembers, ArrayList everyOne) {
        try {
            initComponents();
            windowName.setText(name);
            this.username = uname;
            this.socket = s;
            this.isGroup = isgroup;
            if(name.equals("EVERYONE")){
                isGroup = true;
            }
            this.groupMembers = gMembers;
            this.everyone = everyOne;
            super.setName(name);
            if (!isGroup || name.equals("EVERYONE")) {
                settingsButton.setVisible(false);
                leaveGroupButton.setVisible(false);
            }

            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

            // add default window propertes
            super.addWindowProperties(this);
        } catch (IOException ex) {
            Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {//GEN-BEGIN:initComponents

        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        jScrollPane4 = new javax.swing.JScrollPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        windowName = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        messageTA = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jScrollPane5 = new javax.swing.JScrollPane();
        shoutOutsPanel = new javax.swing.JPanel();
        settingsButton = new javax.swing.JButton();
        leaveGroupButton = new javax.swing.JButton();
        AttachButton = new javax.swing.JButton();

        jLabel1.setText("jLabel1");

        jLabel4.setText("jLabel4");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        setLocationByPlatform(true);
        setResizable(false);
        setUndecorated(true);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204), 3));

        windowName.setFont(new java.awt.Font("Segoe UI Light", 0, 24));
        windowName.setText("EVERYONE");

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/close.png"))); // NOI18N
        jButton1.setToolTipText("Close");
        jButton1.setBorderPainted(false);
        jButton1.setContentAreaFilled(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel3.setForeground(new java.awt.Color(204, 204, 204));
        jLabel3.setText("_________________________________________");

        messageTA.setColumns(15);
        messageTA.setFont(new java.awt.Font("Segoe UI Light", 0, 12));
        messageTA.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        messageTA.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                messageTAKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(messageTA);

        jLabel5.setFont(new java.awt.Font("Segoe UI Light", 0, 10));
        jLabel5.setForeground(new java.awt.Color(153, 153, 153));
        jLabel5.setText("Press Enter to Send.");

        jLabel2.setBackground(new java.awt.Color(255, 255, 255));
        jLabel2.setFont(new java.awt.Font("Segoe UI Light", 0, 14));
        jLabel2.setForeground(new java.awt.Color(0, 102, 0));
        jLabel2.setText("C.O.C. Messenger");
        jLabel2.setOpaque(true);

        jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/poll_topic.png"))); // NOI18N

        jScrollPane5.setBorder(null);
        jScrollPane5.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane5.setAutoscrolls(true);
        jScrollPane5.setMaximumSize(new java.awt.Dimension(100, 62));
        jScrollPane5.setMinimumSize(new java.awt.Dimension(100, 62));
        jScrollPane5.setPreferredSize(new java.awt.Dimension(100, 62));

        shoutOutsPanel.setBackground(new java.awt.Color(255, 255, 255));
        shoutOutsPanel.setMaximumSize(new java.awt.Dimension(100, 62));
        shoutOutsPanel.setMinimumSize(new java.awt.Dimension(100, 62));
        shoutOutsPanel.setPreferredSize(new java.awt.Dimension(100, 62));
        jScrollPane5.setViewportView(shoutOutsPanel);

        settingsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/settings.png"))); // NOI18N
        settingsButton.setToolTipText("Settings");
        settingsButton.setBorderPainted(false);
        settingsButton.setContentAreaFilled(false);
        settingsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                settingsButtonActionPerformed(evt);
            }
        });

        leaveGroupButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/undo.png"))); // NOI18N
        leaveGroupButton.setToolTipText("Leave Group");
        leaveGroupButton.setBorderPainted(false);
        leaveGroupButton.setContentAreaFilled(false);
        leaveGroupButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                leaveGroupButtonActionPerformed(evt);
            }
        });
        leaveGroupButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                leaveGroupButtonKeyReleased(evt);
            }
        });

        AttachButton.setBackground(new java.awt.Color(178, 178, 178));
        AttachButton.setFont(new java.awt.Font("Segoe UI Light", 0, 10));
        AttachButton.setText("Attach");
        AttachButton.setToolTipText("Attach File");
        AttachButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        AttachButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AttachButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(windowName, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                        .addComponent(leaveGroupButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(settingsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(1, 1, 1)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(AttachButton, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2))
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(windowName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addComponent(leaveGroupButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
                    .addComponent(settingsButton, 0, 0, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel10)))
                    .addComponent(AttachButton, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.setVisible(false);
//        amtg.setFocusable(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void messageTAKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_messageTAKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            try {
                if (isGroup) {
                    if (windowName.getText().equals("EVERYONE")) {
                        bw.write("fromXtoX " + username + ":GROUPXX1X01 " + windowName.getText() + ":" + username + " said: " + messageTA.getText());
                    } else {
                        String toUsers = "";
                        int i;
                        for (i = 0; i < groupMembers.size(); i++) {
                            toUsers += groupMembers.get(i).toString() + ",";
                        }
                        bw.write("fromXtoX " + username + ":GROUPXX1X01 " + windowName.getText() + ":" + toUsers + ":" + username + " said: " + messageTA.getText());
                    }
                } else {
                    bw.write("fromXtoX " + username + ":" + windowName.getText() + ":" + username + " said: " + messageTA.getText());
                }
                bw.newLine();
                bw.flush();
            } catch (Exception m) {
            }

            JPanel jp = new javax.swing.JPanel();
            jp.setBackground(new java.awt.Color(255, 255, 255));
            jp.setMinimumSize(new java.awt.Dimension(10, 30));
            jp.setPreferredSize(new java.awt.Dimension(20, 30));

            shoutOutsPanel.add(jp);

            JTextArea jta = new javax.swing.JTextArea();
            jta.setBackground(new java.awt.Color(204, 204, 204));
            jta.setColumns(19);
            jta.setEditable(false);
            jta.setFont(new java.awt.Font("Segoe UI Light", 0, 12)); // NOI18N
            jta.setText(messageTA.getText());
            jta.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
            jta.setLineWrap(true);
            jta.setWrapStyleWord(true);

            JScrollPane jsp = new JScrollPane(jta);
            jsp.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
            jsp.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

            panelHeight += (jta.getPreferredSize().height + 20);
            shoutOutsPanel.setPreferredSize(new java.awt.Dimension(50, panelHeight));
            shoutOutsPanel.add(jsp);
            shoutOutsPanel.revalidate();
            shoutOutsPanel.repaint();
            messageTA.setText("");
        }
    }//GEN-LAST:event_messageTAKeyReleased

    private void leaveGroupButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_leaveGroupButtonActionPerformed
        
}//GEN-LAST:event_leaveGroupButtonActionPerformed

    private void settingsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_settingsButtonActionPerformed
        if (isGroup) {
            if (settingsShown) {
                amtg.setUsersOnline(everyone);
                amtg.setVisible(true);
            } else {
                amtg = new addMembersToGroup(everyone, groupMembers, this);
                amtg.setUsersOnline(everyone);
                amtg.setVisible(true);
                settingsShown = true;
            }
        }
}//GEN-LAST:event_settingsButtonActionPerformed

    private void AttachButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AttachButtonActionPerformed
        JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(AttachButton);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                File file = fc.getSelectedFile();
                System.out.println("Sending File: "+file.getName());
                Scanner s = new Scanner(file);
                String r = "";

                // tell server the start of file sending
                bw.write("isFile startFileBufferXXX9999X0123 " + username + " " + file.getName());
                bw.newLine();
                bw.flush();

                // start sending file
                while (s.hasNextLine()) {
                    r = s.nextLine();
                    bw.write("isFile " + r);
                    bw.newLine();
                    bw.flush();
                }

                // tell server that has sent the file
                String sendTo = "";
                if (isGroup) {
                    System.out.println("ISGROUP TRUE");
                    if (windowName.getText().equals("EVERYONE")) {
                        sendTo = "EVERYONE";
                    } else {
                        sendTo += "GROUPXX1X01:"+windowName.getText()+":";
                        if(!groupMembers.isEmpty()){
                            int i;
                            for (i = 0; i < groupMembers.size() - 1; i++) {
                                sendTo += groupMembers.get(i).toString() + ":";
                            }
                            sendTo += groupMembers.get(i).toString();
                        }
                    }
                } else {
                    sendTo += windowName.getText();
                    sendTo += (":"+username);
                }
                bw.write("isFile:EOFXXX9999X0123:" + sendTo);
                bw.newLine();
                bw.flush();

                System.out.println("FileSending Done.");
                this.fileRecieved(username+"_"+file.getName(), username);
            } catch (IOException ex) {
                Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }//GEN-LAST:event_AttachButtonActionPerformed

    private void leaveGroupButtonKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_leaveGroupButtonKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_leaveGroupButtonKeyReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
//                new chat("Everyone").setVisible(true);
            }
        });


    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AttachButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JButton leaveGroupButton;
    private javax.swing.JTextArea messageTA;
    private javax.swing.JButton settingsButton;
    private javax.swing.JPanel shoutOutsPanel;
    private javax.swing.JLabel windowName;
    // End of variables declaration//GEN-END:variables

    @Override
    public void run() {
        String msg = "";
        while (true) {
            try {
                msg = br.readLine();
                if (msg.equals("disconnectfromserverXXX101")) {
                    socket.close();
                    connected = false;
                } else {
                    writeToPanel(msg);
                }
            } catch (Exception h) {
            }
        }
    }

    public void writeToPanel(String s) {
        JTextArea jta1 = new javax.swing.JTextArea();
        jta1.setBackground(new java.awt.Color(102, 255, 102));
        jta1.setColumns(19);
        jta1.setEditable(false);
        jta1.setFont(new java.awt.Font("Segoe UI Light", 0, 12)); // NOI18N
        jta1.setText(s);
        jta1.setLineWrap(true);
        jta1.setWrapStyleWord(true);
        JScrollPane jsp2 = new JScrollPane(jta1);
        jsp2.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        shoutOutsPanel.add(jsp2);

        JPanel jp2 = new javax.swing.JPanel();
        jp2.setBackground(new java.awt.Color(255, 255, 255));
        jp2.setMinimumSize(new java.awt.Dimension(10, 30));
        jp2.setPreferredSize(new java.awt.Dimension(10, 30));
        jsp2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        panelHeight += (jta1.getPreferredSize().height + 20);
        shoutOutsPanel.setPreferredSize(new java.awt.Dimension(50, panelHeight));
        shoutOutsPanel.add(jp2);
        shoutOutsPanel.revalidate();
        shoutOutsPanel.repaint();
    }

    public void fileRecieved(String fileName, String user) {
        System.out.println("file recieved");
        final javax.swing.JButton DownloadButton = new javax.swing.JButton();
        javax.swing.JLabel downloadLabel = new javax.swing.JLabel();

        downloadLabel.setFont(new java.awt.Font("Segoe UI Light", 0, 11)); // NOI18Nc
        downloadLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        downloadLabel.setLabelFor(DownloadButton);

        if (user == this.username) {
            downloadLabel.setText("You uploaded " + fileName + ".");
        } else {
            downloadLabel.setText(user + " uploaded " + fileName + ".");
        }

        downloadLabel.setFocusable(false);
        downloadLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        downloadLabel.setPreferredSize(new java.awt.Dimension(240, 15));
        shoutOutsPanel.add(downloadLabel);

        DownloadButton.setBackground(new java.awt.Color(255, 255, 255));
        DownloadButton.setFont(new java.awt.Font("Segoe UI Light", 0, 11)); // NOI18N
        DownloadButton.setText("Download: "+fileName);
        DownloadButton.setToolTipText("Download File");
        DownloadButton.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));
        DownloadButton.setPreferredSize(new java.awt.Dimension(240, 17));
        DownloadButton.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseEntered(java.awt.event.MouseEvent evt) {
                DownloadButton.setBackground(new java.awt.Color(204, 204, 204));
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                DownloadButton.setBackground(new java.awt.Color(255, 255, 255));
            }
        });
        DownloadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
                    bw.write(DownloadButton.getText()+":"+username);
                    bw.newLine();
                    bw.flush();
                } catch (IOException ex) {
                    Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });

        shoutOutsPanel.add(DownloadButton);

        shoutOutsPanel.revalidate();
        shoutOutsPanel.repaint();

        panelHeight += 50;
        shoutOutsPanel.setPreferredSize(new java.awt.Dimension(50, panelHeight));
    }

    public String getWindowName() {
        return windowName.getText();
    }

    void setGroupMembers(ArrayList gMembers) {
        try {
            groupMembers = gMembers;
            String message = "newGroupMembers " + windowName.getText();
            for (int i = 0; i < gMembers.size(); i++) {
                message += ":" + gMembers.get(i);
            }
            message += ":" + username;
            bw.write(message);
            bw.newLine();
            bw.flush();
        } catch (IOException ex) {
            Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void userDisconnected(){
        javax.swing.JLabel nLabel = new javax.swing.JLabel();
        nLabel.setFont(new java.awt.Font("Segoe UI Light", 0, 11));
        nLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        nLabel.setText(windowName.getText()+" has logged out.");
        nLabel.setFocusable(false);
        nLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        nLabel.setPreferredSize(new java.awt.Dimension(240, 15));
        shoutOutsPanel.add(nLabel);
        shoutOutsPanel.revalidate();
        shoutOutsPanel.repaint();
        AttachButton.setEnabled(false);
        messageTA.setEnabled(false);
        settingsButton.setEnabled(false);
        leaveGroupButton.setEnabled(false);
    }
}